package com.gxp.system.service.impl;

import com.gxp.common.constant.AutomationConstants;
import com.gxp.common.core.text.Convert;
import com.gxp.common.utils.DateUtils;
import com.gxp.system.domain.ZkPendingExp;
import com.gxp.system.mapper.ZkPendingExpMapper;
import com.gxp.system.service.IZkPendingExpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 自动化实验Service业务层处理
 * 
 * @author zh
 * @date 2024-03-18
 */
@Service
public class ZkPendingExpServiceImpl implements IZkPendingExpService 
{

    private static final Logger log = LoggerFactory.getLogger(ZkPendingExpServiceImpl.class);

    @Autowired
    private ZkPendingExpMapper zkPendingExpMapper;

    /**
     * 查询自动化实验
     * 
     * @param expId 自动化实验主键
     * @return 自动化实验
     */
    @Override
    public ZkPendingExp selectZkPendingExpByExpId(Long expId)
    {
        return zkPendingExpMapper.selectZkPendingExpByExpId(expId);
    }

    /**
     * 查询自动化实验列表
     * 
     * @param zkPendingExp 自动化实验
     * @return 自动化实验
     */
    @Override
    public List<ZkPendingExp> selectZkPendingExpList(ZkPendingExp zkPendingExp)
    {
        return zkPendingExpMapper.selectZkPendingExpList(zkPendingExp);
    }

    /**
     * 新增自动化实验
     * 
     * @param zkPendingExp 自动化实验
     * @return 结果
     */
    @Override
    public int insertZkPendingExp(ZkPendingExp zkPendingExp)
    {
        zkPendingExp.setCreateTime(DateUtils.getNowDate());
        return zkPendingExpMapper.insertZkPendingExp(zkPendingExp);
    }

    /**
     * 修改自动化实验
     * 
     * @param zkPendingExp 自动化实验
     * @return 结果
     */
    @Override
    public int updateZkPendingExp(ZkPendingExp zkPendingExp)
    {
        return zkPendingExpMapper.updateZkPendingExp(zkPendingExp);
    }

    /**
     * 批量删除自动化实验
     * 
     * @param expIds 需要删除的自动化实验主键
     * @return 结果
     */
    @Override
    public int deleteZkPendingExpByExpIds(String expIds)
    {
        return zkPendingExpMapper.deleteZkPendingExpByExpIds(Convert.toStrArray(expIds));
    }

    /**
     * 删除自动化实验信息
     * 
     * @param expId 自动化实验主键
     * @return 结果
     */
    @Override
    public int deleteZkPendingExpByExpId(Long expId)
    {
        return zkPendingExpMapper.deleteZkPendingExpByExpId(expId);
    }

    @Override
    public ZkPendingExp selectZkPendingExpByExpcode(String expcode) {
        return zkPendingExpMapper.selectZkPendingExpByExpcode(expcode);
    }

    /**
     * 第一次响应处理
     * @param expcodes
     */
    @Override
    public void pendingexpRes(String expcodes) {
        if (expcodes == null || expcodes.isEmpty()){
            log.error("expcodes == null");
            return;
        }
        String[] expcodeArr = expcodes.split(",");
        for (String expcode : expcodeArr) {
//            if(expcode.startsWith("[")){
//                expcode = expcode.substring(1);
//            }
//            if(expcode.endsWith("]")){
//                expcode = expcode.substring(0, expcode.length() -1);
//            }
            System.out.println("expcode = " + expcode);
            ZkPendingExp pendingExp = zkPendingExpMapper.selectZkPendingExpByExpcode(expcode);
            Integer readCount = pendingExp.getReadCount();
            readCount++;
            if(readCount > AutomationConstants.MAX_READ_COUNT){
                log.error("readCount > 3, readCount = " + readCount);
                zkPendingExpMapper.deletePendingExpByExpcode(expcode);
            }else {
                ZkPendingExp exp = new ZkPendingExp();
                exp.setExpId(pendingExp.getExpId());
                exp.setReadCount(readCount);
                zkPendingExpMapper.updateZkPendingExp(exp);
            }
        }
    }

    /**
     * 第二次响应处理
     * @param expcode
     * @param result
     */
    @Override
    public void submitAutoExpRes(String expcode, Integer result) {
        if(result!=null){
            if(result == 1){
                System.out.println("自动化实验处理成功");
                zkPendingExpMapper.deletePendingExpByExpcode(expcode);
            }else if (result == 2){
                System.out.println("自动化实验处理失败");
                zkPendingExpMapper.deletePendingExpByExpcode(expcode);
            }
        }
    }
}
